<?php

class base_m extends CI_Model {

	public function __construct() {
		parent::__construct();
		$this->load->database();
	}

	public function getTotal($table = '', $fixed = 0, $category_id = 0, $subcategory_id = 0) 
	{
		$sql = "SELECT * FROM $table WHERE fixed = $fixed";
		if($category_id != 0) 
		{
			$sql .= " AND category_id = $category_id";
		}
		if($subcategory_id != 0)
		{
			$sql .= " AND subcategory_id = $subcategory_id";
		}
		$query = $this->db->query($sql);
		return $query->num_rows();
	}

	public function getPaging($table = '', $fixed = 0, $category_id = 0, $subcategory_id = 0, $page_item = 0, $page_current = 0)
	{
		$sql = "SELECT * FROM $table WHERE fixed = $fixed";
		if($category_id != 0) 
		{
			$sql .= " AND category_id = $category_id";
		}
		if($subcategory_id != 0)
		{
			$sql .= " AND subcategory_id = $subcategory_id";
		}
		$sql .= " ORDER BY id DESC LIMIT $page_item,$page_current";
		$query = $this->db->query($sql);
		return $query->result();
	}

	public function getNews($table = '', $id = 0, $category_id = 0, $subcategory_id = 0, $offset = 0, $limit = 0, $orderBy = '', $fixed = 0) 
	{
		$sql = "SELECT * FROM $table WHERE status = 0";
		if($id != 0)
		{
			$sql .= " AND id = $id";
		}
		if($category_id != 0) 
		{
			$sql .= " AND category_id = $category_id";
		}
		if($subcategory_id != 0)
		{
			$sql .= " AND subcategory_id = $subcategory_id";
		}
		$sql .= " AND fixed = $fixed";
		if($orderBy != "")
		{
			$sql .= " ORDER BY $orderBy";
		}
		if($offset != 0) {
			if($limit != 0)
			{
				$sql .= " LIMIT $offset, $limit";
			}
		}
		else 
		{
			if($limit != 0)
			{
				$sql .= " LIMIT $limit";
			}
		}		
		$query = $this->db->query($sql);
		if($limit == 1)
		{
			return $query->row();
		} 
		else
		{ 
			return $query->result();
		}
	}

	public function getRelatedNews($table = '', $id = 0, $category_id = 0, $subcategory_id	= 0, $limit = 0)
	{
		$sql = "SELECT * FROM $table WHERE status = 0";
		if($id != 0)
		{
			$sql .= " AND id != $id";
		}
		if($category_id != 0) 
		{
			$sql .= " AND category_id = $category_id";
		}
		if($subcategory_id != 0)
		{
			$sql .= " AND subcategory_id = $subcategory_id";
		}
		if($limit != 0)
		{
			$sql .= " LIMIT $limit";
		}
		$query = $this->db->query($sql);
		return $query->result();
	}

	public function getNewsSearch($name = '', $area = 0, $city_id = 0, $fixed = 0)
	{
		$sql = "SELECT news.* FROM news, city WHERE city_id = city.id AND status = 0";
		if($name != '')
		{
			$sql .= " AND news.name LIKE '%%".$name."%%'";
		}
		if($area != 0)
		{
			$sql .= " AND city.area = $area";
		}
		if($city_id != 0)
		{
			$sql .= " AND city_id = $city_id";
		}
		$sql .= " AND fixed = $fixed";
		$query = $this->db->query($sql);
		return $query->result();
	}

	public function getMemberSearch($name = '', $area = 0, $city_id = 0, $fixed = 0)
	{
		$sql = "SELECT users.* FROM users, city WHERE city_id = city.id AND status = 0";
		if($name != '')
		{
			$sql .= " AND users.username LIKE '%%".$name."%%'";
		}
		if($area != 0)
		{
			$sql .= " AND city.area = $area";
		}
		if($city_id != 0)
		{
			$sql .= " AND city_id = $city_id";
		}
		$sql .= " AND fixed = $fixed";
		$query = $this->db->query($sql);
		return $query->result();
	}

	public function getExtension($str) {
        $i = strrpos($str, ".");
        if (!$i) {
            return "";
        }
        $l = strlen($str) - $i;
        $ext = substr($str, $i + 1, $l);
        return $ext;
    }

}